//
//  IntegralAccountsVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class IntegralAccountsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    var type: String = ""
    
    var modelList: myIntegraInfo?{
        didSet{
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        let cellNib = UINib(nibName: "MyIntegralCell", bundle: nil)
        
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "MyIntegralCell")
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        self.getIntegralInfo(type)
    }
    //MARK:UITableViewDataSource
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        if self.modelList?.obj.count == nil{
            return 0
        }else{
            return (self.modelList?.obj.count)!
        }
        
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let sectionNum = self.modelList?.obj[section].onemonthList.count
        return sectionNum!
        
    }
    
    func tableView(tableView: UITableView,
                     heightForHeaderInSection section: Int) -> CGFloat{
        return 20.0
        
    }
    
    func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        
       let header = UILabel (frame:CGRectMake(0,0,SCREEN_WIDTH,15))
       header.backgroundColor = UIColor.lightGrayColor()
       header.text = self.modelList?.obj[section].nowdate
      header.font = UIFont.systemFontOfSize(11)
      
       return header
        
    }
    
   
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 55.0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MyIntegralCell", forIndexPath: indexPath) as! MyIntegralCell
       let model = self.modelList?.obj[indexPath.section].onemonthList[indexPath.row]
        cell.dayLbl.text = model?.noday
        cell.weekLbl.text = String(format: "周%@",(model?.noweek)!)
        cell.messageLbl.text = model?.message
        cell.balanceLbl.text = model?.balance
        if model?.changgetype == "+"{
            cell.changLbl.textColor = UIColor.redColor()
        }else{
            cell.changLbl.textColor = UIColor.blackColor()
        }
        cell.changLbl.text = String(format: "%@%@",(model?.changgetype)!,(model?.changenum)!)
    
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
    }
    
    func getIntegralInfo(type: String){

        DataTool.myIntegral(type, completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
            })

        
    }
    
   
}
